/**
 * 武汉天瑞兴科技有限公司
 */
package com.adds.lvds.service.impl.dict;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.adds.lvds.core.utils.CommUtils;
import com.adds.lvds.dao.impl.dict.DictInfoMapper;
import com.adds.lvds.model.dict.SysDicttype;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 项目名称 ：adds-service-impl
 * 
 * @author zhangjian
 * @time 2018-3-15下午2:45:41
 * @desc 数据字典
 */
@Service("dictInfoService")
public class DictInfoService {
	@Autowired
	private DictInfoMapper dictInfoMapper;
   public SysDicttype querySysDicttypeByMap(
			Map<String, Object> map) {
	   SysDicttype sysDicttype = null;
		try {
			sysDicttype = dictInfoMapper.querySysDicttypeByMap(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sysDicttype;
   }
	/**
	 * 查询当前数据字典及以下数据字典数据
	 * 
	 * @param String
	 * @return
	 */
	public List<SysDicttype> findSysDicttypeByMap(
			Map<String, Object> map) {
		List<SysDicttype> list = null;
		try {
			list = dictInfoMapper.findSysDicttypeByMap(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * 根据字段id，获取字段下面的子节点数据
	 * @param map
	 * @return
	 */
    public SysDicttype getSysDicttypeByMap(
			Map<String, Object> map) {
		SysDicttype sysDicttype = null;
		try {
			List<SysDicttype>	list = dictInfoMapper.findSysDicttypeByMap(map);
			if(!list.isEmpty()) {
				sysDicttype=list.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sysDicttype;
	}
	/**
	 * 根据条件获取数据
	 * 
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public List<SysDicttype> findChildDictidInfoById(
			String parent_dictid) {
		List<SysDicttype> list = null;
		try {
			list = dictInfoMapper.findChildDictidInfoById(parent_dictid);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 根据当前节点获取所有的下级节点数据
	 * 
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public List<SysDicttype> findChildByMap(Map<String, Object> map) {
		List<SysDicttype> list = null;
		try {
			list = dictInfoMapper.findChildByMap(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 录入数据字典数据
	 * 
	 * @param sysDicttype
	 * @throws Exception
	 */
	public Map<String, Object> insertSysDicttype(
			SysDicttype sysDicttype) {
		Map<String, Object> map = new HashMap<>();
		String result = CommUtils.SUCC;
		try {
			dictInfoMapper.insertSysDicttype(sysDicttype);
		} catch (Exception e) {
			result = CommUtils.ERROR;
			e.printStackTrace();
		}
		map.put("result", result);
		map.put("taw_system_dicttype", sysDicttype);
		return map;
	}

	/**
	 * 编辑数据字典数据
	 * 
	 * @param sysDicttype
	 * @throws Exception
	 */
	public String updateSysDicttype(
			SysDicttype sysDicttype) {
		String result = CommUtils.SUCC;
		try {
			dictInfoMapper.updateSysDicttype(sysDicttype);
		} catch (Exception e) {
			result = CommUtils.ERROR;
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 删除操作
	 * 
	 * @param list
	 * @throws Exception
	 */
	public String deleteSysDicttypeByMap(List<SysDicttype> list) {
		String result = CommUtils.SUCC;
		try {
			dictInfoMapper.deleteSysDicttypeByMap(list);
		} catch (Exception e) {
			result = CommUtils.ERROR;
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 加载数据字典树
	 * 
	 * @param dictid
	 * @param initFlag
	 * @return
	 */
	public List<Object> getDictJson(String dictid) {
		List<Object> treeList = new ArrayList<Object>();
		
		Map<String, Object> map = new HashMap<String, Object>();
		if (StringUtils.isEmpty(dictid)) {
			map.put("parent_dictid", "-999");
		} else {
			map.put("parent_dictid", dictid);
		}
		try {
			// 初始化时 自动添加根节点及下级机构
			List<SysDicttype> list = dictInfoMapper
					.findSysDicttypeByMap(map);
			if (list != null && list.size() > 0) {
				for (SysDicttype sysDicttype : list) {
					List<Object> childList = new ArrayList<Object>();
					JSONObject jsonObject_parent = new JSONObject();
					jsonObject_parent
							.put("id", sysDicttype.getDictid());
					jsonObject_parent.put("name",
							sysDicttype.getDictname());
					jsonObject_parent.put("pId",
							sysDicttype.getParent_dictid());
					// 下级节点数据
					List<SysDicttype> childDict = new ArrayList<SysDicttype>();
					childDict = sysDicttype.getChildDicts();
					if (childDict.size() > 0) {
						jsonObject_parent.put("isParent", true);
						//jsonObject_parent.put("open", true);
						for (SysDicttype taw_system_dicttype1 : childDict) {
							JSONObject dictObject = new JSONObject();
							dictObject.put("id",
									taw_system_dicttype1.getDictid());
							dictObject.put("name",
									taw_system_dicttype1.getDictname());
							dictObject.put("pId",
									taw_system_dicttype1.getParent_dictid());

							if (dictInfoMapper.findChildDictidInfoById(
									taw_system_dicttype1.getDictid() + "")
									.size() > 0) {
								dictObject.put("isParent", true);
							}
							childList.add(dictObject);
						}
					}
					JSONArray childJson = JSONArray.fromObject(childList);
					jsonObject_parent.put("children", childJson);
					treeList.add(jsonObject_parent);

				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return treeList;
	}
}
